
package hypermediaresearch.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/*
 * VinhJune 2012
 */
import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;


import hypermediaresearch.meta.PartnerMeta;
import hypermediaresearch.meta.ProjectMeta;
import hypermediaresearch.model.Partner;
import hypermediaresearch.model.Project;
import hypermediaresearch.model.ProjectPartner;


public class ProjectService {
    
    private ProjectMeta p = new ProjectMeta();
    private PartnerMeta partnerMeta = new PartnerMeta();
    
    public Project addProject(Map<String, Object> input, 
                                String coordinator,
                                String partner1,
                                String partner2,
                                String partner3,
                                String partner4){
        Project project = new Project();
        BeanUtil.copy(input, project);
        if (coordinator != ""){
            Partner partner = Datastore.query(partnerMeta)
                                        .filter(p.name.equal(coordinator))
                                        .asSingle();
                                        
            project.getCoordinatorRef().setModel(partner);
        }
        ProjectPartner pp1 = new ProjectPartner();
        if (partner1 != ""){
            Partner p1 = Datastore.query(partnerMeta)
                                        .filter(p.name.equal(partner1))
                                        .asSingle();
            pp1.getProjectRef().setModel(project);
            pp1.getPartnerRef().setModel(p1);
        }
        
        ProjectPartner pp2 = new ProjectPartner();
        if (partner2 != "") {
            Partner p2 = Datastore.query(partnerMeta)
                                        .filter(p.name.equal(partner2))
                                        .asSingle();
            pp2.getProjectRef().setModel(project);
            pp2.getPartnerRef().setModel(p2);
        }
        
        ProjectPartner pp3 = new ProjectPartner();
        if (partner3 != "") {
            Partner p3 = Datastore.query(partnerMeta)
                                        .filter(p.name.equal(partner3))
                                        .asSingle();
            pp3.getProjectRef().setModel(project);
            pp3.getPartnerRef().setModel(p3);
        }
        
        ProjectPartner pp4 = new ProjectPartner();
        if (partner4 != "") {
            Partner p4 = Datastore.query(partnerMeta)
                                        .filter(p.name.equal(partner4))
                                        .asSingle();
            pp4.getProjectRef().setModel(project);
            pp4.getPartnerRef().setModel(p4);
        }
//        Transaction tx = Datastore.beginTransaction();
//        Datastore.put(project);
        Datastore.put(project, pp1, pp2, pp3, pp4);
//        tx.commit();
        System.out.println(project.getName() + " | " 
                            + project.getContent() + " | " 
                            + project.getLocation() +" | " 
                            + project.getYear() );
        return project;
    }
    
    public List<Project> getProjectList(){
        return Datastore.query(p).sort(p.name.desc).asList();
    }
    
    public List<Project> getProjectByYear(String sYear) {
        
        return Datastore.query(p)
                .filter(p.year.equal(sYear))
                .sort(p.name.desc)
                .asList();
        
    }
    
    public Project getProjectByName(String input){
        return Datastore.query(p)
                .filter(p.name.equal(input))
                .asSingle();
    }
    
    public Partner getCoordinator(String input){
        Project project = getProjectByName(input);
        Partner coordinator = project.getCoordinatorRef().getModel();
        return coordinator;
    }
    
    public List<Partner> getPartnersByProject(String input){
        Project project = getProjectByName(input);
        List<Partner> partnerList = new ArrayList<Partner>();
        for (ProjectPartner pp : project.getProjectPartnerListRef().getModelList()) {
            Partner partner = pp.getPartnerRef().getModel();
            partnerList.add(partner);
        }
        return partnerList;
    }
    
    public void deleteProjectByKey(Key key) {
        Datastore.delete(key);
    }
    
    public void deleteProject(Project project){
        Key key = project.getKey();
        Datastore.delete(key);
    }
}
